<template>
  <div class="app-container">
    <div class="head-container">
      <el-button
        class="filter-item"
        size="mini"
        type="primary"
        icon="el-icon-plus"
        @click="add">新增
      </el-button>
    </div>
    <eForm ref="form" :is-add="isAdd"/>
    <el-table v-loading="loading" :data="data" size="small" style="width: 100%;" border>
      <el-table-column prop="ord" label="排序号" width="80"/>
      <el-table-column label="轮播图">
        <template slot-scope="scope">
          <el-image
            style="width: 400px; height: 100px"
            :src="imageHandle(scope.row.imageUrl)">
            <div slot="error" class="image-slot">
              <i class="el-icon-picture-outline"></i>
            </div>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" width="150">
        <template slot-scope="scope">
          <div v-for="item in statusInfo" :key="item.value">
            <el-tag v-if="scope.row.status === item.value" :type="scope.row.status ? '' : 'info'">{{ item.label }}
            </el-tag>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="remark" label="备注信息" width="150"/>
      <el-table-column label="操作" width="150" align="center">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" icon="el-icon-edit" @click="edit(scope.row)"/>
          <el-popover
            :ref="scope.row.id"
            placement="top"
            width="180">
            <p>确定删除本条数据吗？</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[scope.row.id].doClose()">取消</el-button>
              <el-button :loading="delLoading" type="primary" size="mini" @click="subDelete(scope.row.id)">确定
              </el-button>
            </div>
            <el-button slot="reference" type="danger" icon="el-icon-delete" size="mini"/>
          </el-popover>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :total="total"
      :current-page="page + 1"
      style="margin-top: 8px;"
      layout="total, prev, pager, next, sizes"
      @size-change="sizeChange"
      @current-change="pageChange"/>
  </div>
</template>

<script>
  import initData from '@/mixins/initData'
  import eForm from './from'
  import { del } from '@/api/business/banner'

  export default {
    components: { eForm },
    name: 'index',
    mixins: [initData],
    data() {
      return {
        isAdd: true,
        delLoading: false,
        statusInfo: [
          { value: 0, label: '禁用' },
          { value: 1, label: '启用' }
        ]
      }
    },
    created() {
      this.$nextTick(() => {
        this.init()
      })
    },
    methods: {
      beforeInit() {
        this.url = 'web/banner/page-list'
        this.params = { page: this.page, size: this.size }
        return true
      },
      add() {
        this.isAdd = true
        this.$refs.form.dialog = true
      },
      edit(row) {
        this.isAdd = false
        this.$refs.form.coverImg = this.imageHandle(row.imageUrl)
        this.$refs.form.status = row.status === 1
        this.$refs.form.form = {
          id: row.id,
          imageUrl: row.imageUrl,
          status: row.status,
          remark: row.remark,
          type: row.type,
          ord: row.ord
        }
        this.$refs.form.dialog = true
      },
      imageHandle(row) {
        if(row!="" && row!=null){
          let rowObj = JSON.parse(row)
          return rowObj[0].url
        }
      },
      subDelete(id) {
        this.delLoading = true
        del(id).then(res => {
          this.$notify({
            title: '删除成功',
            type: 'success',
            duration: 2500
          })
          this.init()
        })
        this.delLoading = false
        this.$refs[id].doClose()
      }

    }
  }
</script>
